package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.czz.common.dto.common.NameValue;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.utils.CopyUtils;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.common.vo.admin.roleSpace.RoleSpaceFormVo;
import com.wei.czz.common.vo.admin.roleSpace.RoleSpaceVo;
import com.wei.czz.common.vo.common.UpdateStatusVo;
import com.wei.czz.framework.admin.dao.RoleSpaceDao;
import com.wei.czz.framework.admin.entity.RoleSpaceEntity;
import com.wei.czz.framework.admin.service.RoleSpaceService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;
import java.util.function.Function;


/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-10-22 16:45:50
 * className: RoleSpaceServiceImpl 系统角色授权组服务接口实现类
 * version: 1.0
 * description:
 */
@Service("roleSpaceService")
@AllArgsConstructor
public class RoleSpaceServiceImpl extends ServiceImpl<RoleSpaceDao, RoleSpaceEntity> implements RoleSpaceService {

    private static final Logger log = LoggerFactory.getLogger(RoleSpaceServiceImpl.class);

    @Override
    public void save(RoleSpaceVo roleSpaceVo) {
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        RoleSpaceEntity roleSpace = new RoleSpaceEntity();
        roleSpace.setName(roleSpaceVo.getName())
                .setRemark(Optional.ofNullable(roleSpaceVo.getRemark()).orElse(StringUtils.EMPTY));

        if (Objects.isNull(roleSpaceVo.getId())) {
            // 添加
            roleSpace.setStatus(CommonEnum.ZERO.getValue())
                    .setDeleteStatus(CommonEnum.ZERO.getValue())
                    .setCreateTime(date)
                    .setCreateUser(userPo.getUsername())
                    .setCreateUserId(userPo.getUserId())
                    .setUpdateTime(date)
                    .setUpdateUser(StringUtils.EMPTY)
                    .setUpdateUserId(CommonEnum.ZERO.getLongValue());
            int count = baseMapper.insert(roleSpace);
            log.info("添加角色授权组完成。count={}", count);

        } else {
            /*
                获取角色授权组
             */
            RoleSpaceEntity _roleSpace = baseMapper.selectById(roleSpaceVo.getId());
            if (Objects.isNull(_roleSpace)) {
                log.info("角色授权组不存在");
                throw new CzzException(ResultEnum.NOT_FOUND);
            }

            // 修改
            roleSpace.setId(roleSpaceVo.getId())
                    .setUpdateTime(date)
                    .setUpdateUser(userPo.getUsername())
                    .setUpdateUserId(userPo.getUserId());
            int count = baseMapper.updateById(roleSpace);
            log.info("修改角色授权组完成。count={}", count);
        }
    }

    @Override
    public PageDto<RoleSpaceEntity> getPageList(RoleSpaceFormVo formVo) {

        // 创建mp分页对象
        Page<RoleSpaceEntity> page = new Page<>(formVo.getPage(), formVo.getLimit());

        // 参数过滤
        LambdaQueryWrapper<RoleSpaceEntity> roleSpaceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleSpaceLambdaQueryWrapper.like(StringUtils.isNotBlank(formVo.getWord()), RoleSpaceEntity::getName, formVo.getWord())
                .eq(Objects.nonNull(formVo.getStatus()), RoleSpaceEntity::getStatus, formVo.getStatus())
                .eq(RoleSpaceEntity::getDeleteStatus, formVo.getDeleteStatus());
        // 排序
        roleSpaceLambdaQueryWrapper.orderByDesc(RoleSpaceEntity::getUpdateTime);

        // 分页查询
        baseMapper.selectPage(page, roleSpaceLambdaQueryWrapper);

        // 封装返回
        return new PageDto<>(page);
    }

    @Override
    public List<NameValue<Long>> getSimpleList() {
        // 构造过滤条件
        LambdaQueryChainWrapper<RoleSpaceEntity> roleSpaceLambdaQueryWrapper = lambdaQuery()
                .select(RoleSpaceEntity::getId, RoleSpaceEntity::getName)
                .eq(RoleSpaceEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<RoleSpaceEntity> roleSpaceList = baseMapper.selectList(roleSpaceLambdaQueryWrapper);
        log.info("查询角色授权组简单数据完成。list.size={}", roleSpaceList.size());
        Function<RoleSpaceEntity, NameValue<Long>> function = roleSpace ->
                new NameValue<>(roleSpace.getName(), roleSpace.getId());
        // 封装返回
        return CopyUtils.mapList(roleSpaceList, function);
    }

    @Override
    public void updateStatus(UpdateStatusVo updateStatusVo) {
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<RoleSpaceEntity> roleSpaceLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        roleSpaceLambdaUpdateWrapper.set(RoleSpaceEntity::getStatus, updateStatusVo.getStatus())
                .set(RoleSpaceEntity::getUpdateTime, date)
                .set(RoleSpaceEntity::getUpdateUser, userPo.getUsername())
                .set(RoleSpaceEntity::getUpdateUserId, userPo.getUserId());
        roleSpaceLambdaUpdateWrapper.in(RoleSpaceEntity::getId, updateStatusVo.getIdList())
                .eq(RoleSpaceEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 更新
        int count = baseMapper.update(roleSpaceLambdaUpdateWrapper);
        log.info("修改角色授权组状态完成。count={}", count);
    }

    @Override
    public void delete(List<Long> idList) {
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<RoleSpaceEntity> roleSpaceLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        roleSpaceLambdaUpdateWrapper.set(RoleSpaceEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(RoleSpaceEntity::getUpdateTime, date)
                .set(RoleSpaceEntity::getUpdateUser, userPo.getUsername())
                .set(RoleSpaceEntity::getUpdateUserId, userPo.getUserId())
                .in(RoleSpaceEntity::getId, idList)
                .eq(RoleSpaceEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(roleSpaceLambdaUpdateWrapper);
        log.info("删除角色授权组完成。count={}", count);
    }
}